<?php

class Model_Media extends Model
{
    var $name = null;
    
	function __construct($id = 0)
	{
		parent::__construct();
		$sName = 'banners_media';
		$this->name = $sName;
		$this->properties[$sName] = array(
		    'id',
            'banner_id',
            'image',
			'flash',
			'cdate',
			'mdate',
		);
		$this->cdate = time();
		if ($id)
		{
		    $this->getBy('id', $id);
		}
	}
		
	public function addMassMedia($iBannerId)
	{
	    $aFiles = $_FILES;
	    if (isset($aFiles['image']) && isset($aFiles['flash']) && $aFiles['image'] && $aFiles['flash'])
	    {
	        foreach ($aFiles['image']['name'] as $k => $v)
	        {
	            $aImage = array(
	               'name'     => $aFiles['image']['name'][$k],
	               'type'     => $aFiles['image']['type'][$k],
	               'tmp_name' => $aFiles['image']['tmp_name'][$k],
	               'error'    => $aFiles['image']['error'][$k],
	               'size'     => $aFiles['image']['size'][$k],
	            );
	            $aFlash = array(
	               'name'     => $aFiles['flash']['name'][$k],
	               'type'     => $aFiles['flash']['type'][$k],
	               'tmp_name' => $aFiles['flash']['tmp_name'][$k],
	               'error'    => $aFiles['flash']['error'][$k],
	               'size'     => $aFiles['flash']['size'][$k],
	            );
	            $sImageName = $sFlashName = '';
	            if ($aImage['name'] && intval($aImage['size']) > 0 && !$aImage['error'])
	            {
	                $sExt = strtolower(end(explode(".", $aImage['name'])));
	                if (in_array($sExt, UploadMedia::$aImageTypes))
	                {
	                    $sImageName = sha1(rand().md5(time()).rand()).'.'.$sExt;
	                    UploadMedia::Save('image', $iBannerId.'/'.$sImageName, $k);
	                }
	            }
	            if ($aFlash['name'] && intval($aFlash['size']) > 0 && !$aFlash['error'])
	            {
	                $sExt = strtolower(end(explode(".", $aFlash['name'])));
	                if ($sExt == 'swf')
	                {
	                    $sFlashName = sha1(rand().md5(time()).rand()).'.'.$sExt;
	                    UploadMedia::Save('flash', $iBannerId.'/'.$sFlashName, $k);
	                }
	            }
	            if ($sImageName || $sFlashName)
	            {
	                $iTime = time();
	                $aData = array(
	                   'banner_id' => $iBannerId,
	                   'image'     => $sImageName,
	                   'flash'     => $sFlashName,
	                   'cdate'     => $iTime,
	                   'mdate'     => $iTime,
	                );
	                $media = new Model_Media();
	                $media->values = $aData;
	                $media->save();
	            }
	        }
	    }
	}

    public function editMassMedia($iBannerId)
	{
        $this->addMassMedia($iBannerId);
	    $aFiles = $_FILES;
	    if (isset($aFiles['eimage']) && isset($aFiles['eflash']) && $aFiles['eimage'] && $aFiles['eflash'])
	    {
	        foreach ($aFiles['eimage']['name'] as $k => $v)
	        {
	            $aImage = array(
	               'name'     => $aFiles['eimage']['name'][$k],
	               'type'     => $aFiles['eimage']['type'][$k],
	               'tmp_name' => $aFiles['eimage']['tmp_name'][$k],
	               'error'    => $aFiles['eimage']['error'][$k],
	               'size'     => $aFiles['eimage']['size'][$k],
	            );
	            $aFlash = array(
	               'name'     => $aFiles['eflash']['name'][$k],
	               'type'     => $aFiles['eflash']['type'][$k],
	               'tmp_name' => $aFiles['eflash']['tmp_name'][$k],
	               'error'    => $aFiles['eflash']['error'][$k],
	               'size'     => $aFiles['eflash']['size'][$k],
	            );

	            if ($aImage['name'] && intval($aImage['size']) > 0 && !$aImage['error'])
	            {
	                $sExt = strtolower(end(explode(".", $aImage['name'])));
	                if (in_array($sExt, UploadMedia::$aImageTypes))
	                {
                        $sImageName = DB::scalarSelect('SELECT image FROM banners_media WHERE id = '.intval($k));
                        if (!$sImageName)
                        {
                            $sImageName = sha1(rand().md5(time()).rand()).'.'.$sExt;
                        }
	                    UploadMedia::Save('eimage', $iBannerId.'/'.$sImageName, $k);
                        DB::query('UPDATE banners_media SET image = "'.$sImageName.'" WHERE id = '.intval($k));
	                }
	            }

	            if ($aFlash['name'] && intval($aFlash['size']) > 0 && !$aFlash['error'])
	            {
	                $sExt = strtolower(end(explode(".", $aFlash['name'])));

	                if ($sExt == 'swf')
	                {
                        $sFlashName = DB::scalarSelect('SELECT flash FROM banners_media WHERE id = '.intval($k));

                        if (!$sFlashName)
                        {
                            $sFlashName = sha1(rand().md5(time()).rand()).'.'.$sExt;
                        }
	                    UploadMedia::Save('eflash', $iBannerId.'/'.$sFlashName, $k);
                        DB::query('UPDATE banners_media SET flash = "'.$sFlashName.'" WHERE id = '.intval($k));
	                }
	            }

                $aDel = Request::postVars(array('del_img', 'del_flash'));
                if (is_array($aDel['del_img']) && $aDel['del_img'])
                {
                    foreach ($aDel['del_img'] as $v)
                    {
                        $sImageName = DB::scalarSelect('SELECT image FROM banners_media WHERE id = '.intval($v));
                        UploadMedia::Clear($iBannerId.'/'.$sImageName);
                        DB::query('UPDATE banners_media SET image = "" WHERE id = '.intval($v));
                    }
                }
                if (is_array($aDel['del_flash']) && $aDel['del_flash'])
                {
                    foreach ($aDel['del_flash'] as $v)
                    {
                        $sFlashName = DB::scalarSelect('SELECT flash FROM banners_media WHERE id = '.intval($v));
                        UploadMedia::Clear($iBannerId.'/'.$sFlashName);
                        DB::query('UPDATE banners_media SET flash = "" WHERE id = '.intval($v));
                    }
                }
                DB::query('DELETE FROM banners_media WHERE image = "" AND flash = ""');
	        }
	    }
	}

	function delete()
	{
		UploadMedia::Clear($this->image_name);
		parent::delete();
	}
}